<?php
/**
 * 最后会走到finally
 */
try {
    $error = 'Always throw this error';
    throw new InvalidArgumentException($error);

    // Code following an exception is not executed.
    echo 'Never executed';

} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
} finally {
    echo "finally: finally always executed doesnot care exception\n";
}

echo "\n\n";
/**
 * 使用了return  始终返回finally中的return
 */
function finallyReturn($touchException = TRUE) {
    try {
        if ($touchException) {
            $error = 'Always throw this error';
            throw new InvalidArgumentException($error);

        }
        echo "go on ...\n";
        // Code following an exception is not executed.
        return  'Never executed';
    
    } catch (Exception $e) {
        return 'Caught exception: ' .  $e->getMessage() . "\n";
    } finally {
        return "finally: finally always return doesnot care exception\n";
    }
    return "end\n";
}

$return = finallyReturn();
echo "return: $return";
$return = finallyReturn(false);
echo "return: $return";

